import $router from '@/router'
import VueCookies from "vue-cookies";
import CONFIG from '@/config'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'

/**
 * 退出登录
 */
export function logout() {
  sessionStorage.removeItem("zhanyun.orgName")
  sessionStorage.removeItem("zhanyun.orgId")
  sessionStorage.removeItem("zhanyun.username")
  sessionStorage.removeItem("zhanyun.token")
  sessionStorage.removeItem("zhanyun.permissionList")
  sessionStorage.removeItem("zhanyun.name")
  $router.push('/login')
}

export const setPageSize = pageSize => {
  VueCookies.set('zhanyun_pagesize', pageSize)
}

export const getPageSize = () => {
  const pageSize = VueCookies.get('zhanyun_pagesize')
  //window.console.log("我进来了")
  if(pageSize) {
    return  Number(pageSize)
  } else {
    setPageSize(CONFIG.defaultPageSize)
    return CONFIG.defaultPageSize
  }
}

export const exportExcel = (tableId, fileName, actionFixed) => {
  // 判断要导出的节点中是否有fixed的表格，如果有，转换excel时先将该dom移除，然后append回去，
  const fix = document.querySelector(tableId).querySelector('.el-table__fixed-right');
  let wb;
  if (fix) {
    wb = XLSX.utils.table_to_book(document.querySelector(tableId).removeChild(fix));
    document.querySelector(tableId).appendChild(fix);
  } else {
    wb = XLSX.utils.table_to_book(document.querySelector(tableId));
  }
  const wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'});
  try {
    FileSaver.saveAs(
      new Blob([wbout], {type: 'application/octet-stream'}), fileName+'.xlsx');
  } catch (e) {
    if (typeof console !== 'undefined')window.console.log(e, wbout);
  }
  return wbout;
}
